/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.OperationContent;
import com.kaaction.modules.mer.service.OperationContentService;
import com.kaaction.modules.mer.service.OperationContentTagService;

/**
 * 运营内容Controller
 * @author huanghaiming
 * @version 2017-07-03
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/operationContent")
public class OperationContentController extends BaseController {

	@Autowired
	private OperationContentService operationContentService;
	
	@Autowired
	private OperationContentTagService operationContentTagService;
	
	@ModelAttribute
	public OperationContent get(@RequestParam(required=false) String id) {
		OperationContent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationContentService.get(id);
			entity.setTag(operationContentTagService.selTagId(id));
		}
		if (entity == null){
			entity = new OperationContent();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:operationContent:view")
	@RequestMapping(value = {"list", ""})
	public String list(OperationContent operationContent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OperationContent> page = operationContentService.findPage(new Page<OperationContent>(request, response), operationContent); 
		model.addAttribute("page", page);
		return "modules/mer/operationContentList";
	}
	
	@RequiresPermissions("mer:operationContent:view")
	@RequestMapping(value = "form")
	public String form(OperationContent operationContent, Model model) {
		model.addAttribute("operationContent", operationContent);
		return "modules/mer/operationContentForm";
	}
	
	@RequiresPermissions("mer:operationContent:edit")
	@RequestMapping(value = "save")
	public String save(OperationContent operationContent, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, operationContent)){
			return form(operationContent, model);
		}
		
		String[] tag = operationContent.getTag();
		if(tag.length>3){
			model.addAttribute("tag", "标签不能超过3个");
			return form(operationContent, model);
		}
		
		operationContentService.save(operationContent);
		addMessage(redirectAttributes, "保存运营内容成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationContent/?repage";
	}
	
	@RequiresPermissions("mer:operationContent:edit")
	@RequestMapping(value = "delete")
	public String delete(OperationContent operationContent, RedirectAttributes redirectAttributes) {
		operationContentService.delete(operationContent);
		addMessage(redirectAttributes, "删除运营内容成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationContent/?repage";
	}
	
	@RequiresPermissions("mer:operationContent:edit")
	@RequestMapping(value = "updateIsShow")
	public String updateIsShow(OperationContent operationContent, Model model, RedirectAttributes redirectAttributes) {
		operationContentService.updateIsShow(operationContent);
		return "redirect:"+Global.getAdminPath()+"/mer/operationContent/?repage";
	}
	
	
	@RequiresPermissions("mer:operationContent:view")
	@RequestMapping(value = "operationSubDtlList")
	public String operationSubDtlList(OperationContent operationContent, HttpServletRequest request, HttpServletResponse response, Model model) {
		String  opesubId = operationContent.getOpesubId();
		if(StringUtils.isNotBlank(opesubId)){
			operationContent.setOpesubId(opesubId.split(",")[0]);
		}
		operationContent.setFlag("1");
		Page<OperationContent> page = operationContentService.findPage(new Page<OperationContent>(request, response), operationContent); 
		model.addAttribute("page", page);
		return "modules/mer/operationSubjectDetailForm";
	}

}